const mysql = require('mysql2/promise');

// 数据库连接配置
const dbConfig = {
  host: '47.92.157.229',
  port: 3306,
  user: 'root',
  password: '95zhz?!yzkj2816',
  database: 'freshshop',
  charset: 'utf8mb4',
  connectionLimit: 10
};

// 创建连接池
const pool = mysql.createPool(dbConfig);

// 测试数据库连接
async function testConnection() {
  try {
    const connection = await pool.getConnection();
    console.log('数据库连接成功');
    connection.release();
  } catch (error) {
    console.error('数据库连接失败:', error.message);
  }
}

// 执行查询的通用方法
async function query(sql, params = []) {
  try {
    const [rows] = await pool.execute(sql, params);
    return rows;
  } catch (error) {
    console.error('数据库查询错误:', error.message);
    throw error;
  }
}

module.exports = {
  pool,
  query,
  testConnection
};